package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private SpuInfoMapper spuInfoMapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private SpuPosterMapper spuPosterMapper;



    //获取一级分类数据
    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    //获取二级分类数据
    @Override
    public List<BaseCategory2> getCategory2(long category1Id) {
        //select * from base_category2 where category1_id=1;
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",category1Id);
        return baseCategory2Mapper.selectList(queryWrapper);
    }

    //获取三级分类数据
    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        QueryWrapper<BaseCategory3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id",category2Id);
        return baseCategory3Mapper.selectList(queryWrapper);
    }

    //根据分类Id 获取平台属性集合
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
      return baseAttrInfoMapper.selectAttrInfoList(category1Id, category2Id, category3Id);
    }

    //保存平台属性   也是修改平台属性的控制器
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
//              保存平台属性
//        baseAttrInfoMapper.insert(baseAttrInfo);
//        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
//        for (BaseAttrValue baseAttrValue : attrValueList) {
//            //attrId 传递的时候是null
//            baseAttrValue.setAttrId(baseAttrInfo.getId());
//            baseAttrValueMapper.insert(baseAttrValue);
//        }
        if (baseAttrInfo.getId() == null) {
            //插入平台属性
            baseAttrInfoMapper.insert(baseAttrInfo);
        } else {
            //修改平台属性
            baseAttrInfoMapper.updateById(baseAttrInfo);
        }
        QueryWrapper<BaseAttrValue> valueQueryWrapper = new QueryWrapper<>();
        valueQueryWrapper.eq("attr_id", baseAttrInfo.getId());
        baseAttrValueMapper.delete(valueQueryWrapper);

        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {
            //attrId 传递的时候是null
            baseAttrValue.setAttrId(baseAttrInfo.getId());
            baseAttrValueMapper.insert(baseAttrValue);
        }
    }


    //根据平台属性Id 获取到平台属性值集合
    @Override
    public List<BaseAttrValue> getAttrValueList(long attrId) {
        QueryWrapper<BaseAttrValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_id",attrId);
       return baseAttrValueMapper.selectList(queryWrapper);
    }
    //根据平台属性Id 获取到平台属性值集合
    @Override
    public BaseAttrInfo getAttrInfo(long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);
        if (baseAttrInfo!=null){
            baseAttrInfo.setAttrValueList(getAttrValueList(attrId));
        }
        return baseAttrInfo;
    }

    //spu分页列表
    @Override
    public IPage<SpuInfo> getSpiInfoList(Page<SpuInfo> page1, SpuInfo spuInfo) {
        QueryWrapper<SpuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", spuInfo.getCategory3Id());
        queryWrapper.orderByDesc("id");
        return spuInfoMapper.selectPage(page1, queryWrapper);
    }

    //获取销售属性数据
    @Override
    public List<BaseSaleAttr> baseSaleAttrList() {
       return baseSaleAttrMapper.selectList(null);
    }


    //保存spu
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
         /*
         spuInfo;	商品表
        spuImage;	商品图片表
        spuSaleAttr;	销售属性
        spuSaleAttrValue;	销售属性值
        spuPoster;	商品海报
     */
        spuInfoMapper.insert(spuInfo);
        //商品图片表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)){
            spuImageList.stream().forEach((spuImage)->{
                spuImage.setSpuId(spuInfo.getId());
            spuImageMapper.insert(spuImage);
            });
        }

        //销售属性
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)){
            spuSaleAttrList.stream().forEach((spuSaleAttr)->{
                spuSaleAttr.setSpuId(spuInfo.getId());
                spuSaleAttrMapper.insert(spuSaleAttr);

                //销售属性值
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)){
                    spuSaleAttrValueList.stream().forEach((spuSaleAttrValue) ->{
                        spuSaleAttrValue.setSpuId(spuInfo.getId());
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    });
                }
            });
        }

        //商品海报
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)){
            spuPosterList.stream().forEach((spuPoster) -> {
                spuPoster.setSpuId(spuInfo.getId());
                spuPosterMapper.insert(spuPoster);
            });
        }
    }
}
